<?php
class cmshopapi_AddItem extends cmshopApi_base
{
    var $verb='AddItem';
    var $cmd='Add';
    var $Ack='Success';
    var $fields=array(
        
    );
    // 收取  商品信息 接口
    function Request(){
        if(!$this->checkAuthToken()) throw new Exception_AuthTokenInvalidate('');
        $this->checkinput();
        $goods=Goods::find('store_id=? And seller_id=? And sn=?',$this->token['store_id'],$this->token['seller_id'],$this->stringInput('sn'))->getOne();
//         if(!$goods->isNewRecord()){
//             throw new Exception_GoodSnExist($this->stringInput('sn'));
//         }
        $va=array(
            'app_id'=>$this->token['app_id'],
            'currency'=>$this->stringInput('currency'),
            'market_price'=>$this->stringInput('market_price'),
            'price'=>$this->stringInput('price'),
            'weight'=>$this->stringInput('weight'),
            'stock'=>$this->stringInput('add_stock'),
            'imgurl'=>$this->stringInput('image_url'),
            'category_id'=>$this->stringInput('category'), // x
            'custom'=>$this->stringInput('custom'),
        );
        if(strlen($va['currency'])==0){
            $va['currency']= $this->store->currency;
        }
        if($goods->isNewRecord()){
            $va['store_id']=$this->token['store_id'];
            $va['seller_id']=$this->token['seller_id'];
            $va['token_id']=$this->token['token_id'];
            $va['sn']=$this->stringInput('sn');
        }else{
            $this->cmd='Update';
        }
        $goods->changeProps($va);
        // tag
        $goods->tags=Taggoods::find('[store_id]=? And [tag_name] in(?)',$this->token['store_id'],$this->input['tag'])->getAll();//$this->_context->post('tag_id');
        $goods->save();
        // 
        if(isset($this->input['language_desc']['language_code'])){
            $language_desc[0]=$this->input['language_desc'];
        }else{
            $language_desc=$this->input['language_desc'];
        }
        foreach($language_desc as $language){
            $gld=Goods_Desc::find('goods_id=? And language=?',$goods->goods_id,$language['language_code'])->getOne();
            if(empty($language['title'])) continue;
            $gld->title=$language['title'];
            $gld->desc=$language['desc'];
            $gld->introduction=$language['introduction'];
            $gld->property= Goods::parseprops($language['property']);
            if($gld->isNewRecord()){
                $gld->goods_id=$goods->goods_id;
                $gld->store_id=$goods->store_id;
                $gld->language=$language['language_code'];
            }
            $gld->save();
        }
        
        if($goods->id()){
            return $this->Response($goods);
        }
        return false;
    }
    
    // 回给 客户  .
    function Response($goods,$attach=null){
        $xmlArr=array(
            'Ack'=>$this->Ack,
            'timestamp'=>$this->dateTime(),
            'message'=>$this->cmd.' '.$this->Ack,
		);
        if($this->Ack=='Success'){
            $xmlArr=$xmlArr + array(
    			'item_id'=>$goods->goods_id,
    			'created'=>$this->dateTime($goods->created),
    			
    		);
		}
		if(is_array($attach)){
            $xmlArr=$xmlArr+$attach;
        }
        return $this->xmlresponse($xmlArr);
    }
    // check input 
    function checkinput(){
        if(!is_array($this->input)||count($this->input)<=0) throw new Exception_RequestFormatInvalidate('');
        //  check request fileds ,必需字段 
        foreach(array('sn','price','weight','add_stock','language_desc') as $k){
            if(!isset($this->input[$k])) throw new Exception_RequestFormatInvalidate('');
        }
        // check 全部字段
        foreach($this->input as $k=>$v){
            switch($k){
                case 'sn':
                case 'price':
                case 'weight':
                case 'add_stock':
                case 'language_desc':
                case 'RequesterCredentials':
                    break;
                case 'market_price':
                case 'currency':
                case 'category':
                case 'tag':
                case 'image_url':
                case 'custom':
                    break;
                default: throw new Exception_RequestFormatInvalidate('field name:'.$k.'no found.');
            }
        }
        //Tag 数据
        if(strlen($this->stringInput('tag'))){
            $this->input['tag']=explode(',',$this->stringInput('tag'));
        }
    }
}
class Exception_GoodSnExist extends QException {}